package com.nongda.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.nongda.dto.ChuKuDto;
import com.nongda.dto.LiuShuiChaCunDto;
import com.nongda.dto.LiuShuiDto;
import com.nongda.mapper.TaiZhangMapper;
import com.nongda.pojo.LiuShui;
import com.nongda.pojo.ShangPin;
import com.nongda.result.LiuShuiResult;
import com.nongda.result.PageResult;
import com.nongda.service.TaiZhangService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.util.List;

@Service
@Slf4j
public class TaiZhangServiceImpl implements TaiZhangService {
    @Autowired
    TaiZhangMapper taiZhangMapper;

    @Override
    @Transactional
    public int chuku(ChuKuDto chuKuDto) {
        ChuKuDto chuKuDto_cangku=new ChuKuDto();
        BeanUtils.copyProperties(chuKuDto,chuKuDto_cangku);
        taiZhangMapper.chuku_cangku(chuKuDto);//仓库商品数量减少
        int num=taiZhangMapper.chuku(chuKuDto);//门店商品数量增加
        int num1=taiZhangMapper.chaxun(chuKuDto);//查询门店修改以后的商品数量
        int num2=taiZhangMapper.chukuId(chuKuDto);
        //

        LiuShuiDto liuShuiDto=new LiuShuiDto();
        liuShuiDto.setId(++num2);
      System.out.println(num2);
        liuShuiDto.setShangPinId(chuKuDto.getId());
        liuShuiDto.setUpdateTime(LocalDate.now());
        liuShuiDto.setStatus(1);
        liuShuiDto.setNum(chuKuDto.getNumber());
        //
        taiZhangMapper.dingdan(liuShuiDto);//生成此次出库订单
        System.out.println(num1);
        return num1;
    }

    @Override
    public int ruku(ChuKuDto chuKuDto) {
        taiZhangMapper.ruku_mendian(chuKuDto);//门店商品减少
        taiZhangMapper.ruku(chuKuDto);//仓库数量增加
        int chaxun = taiZhangMapper.chaxun(chuKuDto);
        System.out.println(chaxun);
        int num2=taiZhangMapper.chukuId(chuKuDto);
        //

        LiuShuiDto liuShuiDto=new LiuShuiDto();
        liuShuiDto.setId(++num2);
        System.out.println(num2);
        liuShuiDto.setShangPinId(chuKuDto.getId());
        liuShuiDto.setUpdateTime(LocalDate.now());
        liuShuiDto.setStatus(0);
        liuShuiDto.setNum(chuKuDto.getNumber());
        //
        taiZhangMapper.dingdan(liuShuiDto);//生成此次入库订单
        return chaxun;
    }

    @Override
    public LiuShuiResult pageLiuShui(LiuShuiChaCunDto liuShuiDto) {
       PageHelper.startPage(liuShuiDto.getPage(),liuShuiDto.getPageSize());
       int id1=taiZhangMapper.selectId(liuShuiDto);
       Page<LiuShui> page=taiZhangMapper.pageLiuShui(id1);
       List<LiuShui> result = page.getResult();
        System.out.println(result);
        int num=taiZhangMapper.selectNum(liuShuiDto);
        String price=taiZhangMapper.sekectPrice(liuShuiDto);
        System.out.println(num+"1111111111111111111111111111111111111111");
        System.out.println(price+"1111111111111111111111111111111111111111");
//        taiZhangMapper.pageLiuShui(liuShuiDto);
        return new LiuShuiResult(liuShuiDto.getPage(), liuShuiDto.getPageSize(),num,price,result );
    }
}
